<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>学生列表</title>
    <!--1. 引入bs的css样式库-->
    <link rel="stylesheet" href="/webjars/bootstrap/3.4.1/css/bootstrap.min.css">
    <!--2. 引入jquery的库-->
    <script src="/webjars/jquery/3.6.0/jquery.min.js"></script>
    <!--3. 引入bs的js库-->
    <script src="/webjars/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style>
        .table{
            text-align: center;
        }
        .container{
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">
                <h3>学生列表（Mapper + SpringBoot + Thymeleaf）</h3>
            </h3>
        </div>
        <table class="table table-hover table-striped">
            <tr>
                <td>学号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
                <td>住址</td>
                <td>所在班级</td>
                <td>操作</td>
            </tr>
            <tr th:each="stud : ${pr.rows}">
                <td>[[${stud.sid}]]</td>
                <td th:text="${stud.sname}"></td>
                <td th:text="${stud.sex}"></td>
                <td th:text="${stud.age}"></td>
                <td th:text="${stud.addr}"></td>
                <td th:text="${stud.cname}"></td>
                <td>
                    <a href="#" class="btn btn-info btn-sm" th:onclick="updateUI([[${stud}]])">修改</a>
                    <a th:href="'/student/delete/'+${stud.sid}"  onclick="return confirm('你真的要删除吗？')" class="btn btn-danger btn-sm">删除</a>
                </td>
            </tr>
        </table>
        <div class="panel-footer text-right">
            <!--1. 条件查询-->
            <form class="form-inline" style="float: left" action="/student/search" method="post" name="form1">
                <input type="hidden" id="page" name="page">
                <div class="form-group">
                    <input type="text" th:value="${stud?.sname}" class="form-control" name="sname" placeholder="输入用户名">
                </div>
                <div class="form-group">
                    <input type="text" th:value="${stud?.addr}"  class="form-control" name="addr" placeholder="输入住址">
                </div>
                <select class="form-control" name="cid">
                    <option value="0">所有班级</option>
                    <option th:each="c : ${classes}" th:value="${c.cid}" th:selected="${c.cid==stud?.cid}">[[${c.cname}]]</option>
                </select>
                <button type="submit" class="btn btn-default btn-sm">查询</button>
                <button type="button" class="btn btn-success btn-sm" onclick="addUI()">添加学生</button>
            </form>
            <!--2. 分页导航-->
            <nav aria-label="Page navigation" >
                <ul class="pagination">
                    <li id="first">
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <!--下面的语法可以直接遍历数字-->
                    <li th:each="p : ${#numbers.sequence(1, pr.totalPage)}" th:class="${p == pr.page ? 'active':''}">
                        <a href="#" th:onclick="skipPage([[${p}]])">[[${p}]]</a>
                    </li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
            百年智2022.
        </div>
    </div>


    <!-- 模态框 -->
    <div class="modal fade" id="myModal" >
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="title">添加学生</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" name="form2" action="/student/add" method="post">
                        <input type="hidden" name="sid" id="sid">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">学生姓名:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="sname" id="sname" placeholder="请输入姓名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">学生性别:</label>
                            <div class="col-sm-10">
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="sex" id="sex1" value="男" >男
                                    </label>
                                    <label>
                                        <input type="radio" name="sex" id="sex2" value="女" >女
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">学生年龄:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">学生住址:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="addr" name="addr" placeholder="请输入住址">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">所在班级:</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="cid" id="cid">
                                    <option value="0">所有班级</option>
                                    <option th:each="c : ${classes}" th:value="${c.cid}">[[${c.cname}]]</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="save()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script>

    function addUI(){
        $("#title").html("添加学生")
        $('#myModal').modal('show')
        document.forms[1].reset()
    }
    function updateUI(stud){
        $("#sname").val(stud.sname);
        $("#age").val(stud.age)
        $("#addr").val(stud.addr)
        $("#cid").val(stud.cid)
        $("#sex1").prop("checked",stud.sex == '男' ? true : '')
        $("#sex2").prop("checked",stud.sex == '女' ? true : '')
        $("#sid").val(stud.sid)             // 为隐藏域赋值，是判断修改还是添加的重要关键点？

        $("#title").html("修改学生")

        $('#myModal').modal('show')
    }
    function save(){
        let url="/student/add"
        if($("#sid").val()){
            url="/student/update"
        }
        document.forms[1].action=url
        document.forms[1].submit()

    }

    function skipPage(page) {
        $("#page").val(page);
        document.form1.submit();
    }
</script>